package com.fhminyi.dao;

import com.fhminyi.model.Comment;
import com.fhminyi.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class CommentDao {
    // 获取所有评论
    public List<Comment> getAllComments() throws SQLException {
        List<Comment> comments = new ArrayList<>();
        String sql = "SELECT * FROM 评论";
        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {
                Comment comment = new Comment();
                comment.setCommentId(rs.getInt("评论_id"));
                comment.setProductId(rs.getInt("商品_id"));
                comment.setUserId(rs.getInt("用户_id"));
                comment.setContent(rs.getString("内容"));
                comment.setRating(rs.getInt("评分"));
                comment.setCreateTime(rs.getTimestamp("创建时间"));
                comments.add(comment);
            }
        }
        return comments;
    }

    // 根据评论ID获取评论
    public Comment getCommentById(int commentId) throws SQLException {
        String sql = "SELECT * FROM 评论 WHERE 评论_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, commentId);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    Comment comment = new Comment();
                    comment.setCommentId(rs.getInt("评论_id"));
                    comment.setProductId(rs.getInt("商品_id"));
                    comment.setUserId(rs.getInt("用户_id"));
                    comment.setContent(rs.getString("内容"));
                    comment.setRating(rs.getInt("评分"));
                    comment.setCreateTime(rs.getTimestamp("创建时间"));
                    return comment;
                }
            }
        }
        return null;
    }

    // 添加评论
    public void addComment(Comment comment) throws SQLException {
        String sql = "INSERT INTO 评论 (商品_id, 用户_id, 内容, 评分) VALUES (?, ?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, comment.getProductId());
            pstmt.setInt(2, comment.getUserId());
            pstmt.setString(3, comment.getContent());
            pstmt.setInt(4, comment.getRating());
            pstmt.executeUpdate();
        }
    }

    // 更新评论
    public void updateComment(Comment comment) throws SQLException {
        String sql = "UPDATE 评论 SET 商品_id = ?, 用户_id = ?, 内容 = ?, 评分 = ? WHERE 评论_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, comment.getProductId());
            pstmt.setInt(2, comment.getUserId());
            pstmt.setString(3, comment.getContent());
            pstmt.setInt(4, comment.getRating());
            pstmt.setInt(5, comment.getCommentId());
            pstmt.executeUpdate();
        }
    }

    // 删除评论
    public void deleteComment(int commentId) throws SQLException {
        String sql = "DELETE FROM 评论 WHERE 评论_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, commentId);
            pstmt.executeUpdate();
        }
    }
}